package com.fzz.controller;

import com.fzz.domain.JsonObject;
import com.fzz.domain.User;
import com.fzz.domain.View;
import com.fzz.service.ViewService;
import com.fzz.serviceImpl.ViewServiceImpl;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
public class ViewController {

    @Autowired
    private ViewService viewService=new ViewServiceImpl();

    //分页查询视频
    @RequestMapping(value = "/admin/selectViewWithPage")
    @ResponseBody
    public JsonObject selectViewWithPage(Model model,Integer id,String viewName,@RequestParam(value = "page",defaultValue = "1") int page, @RequestParam(value = "limit",defaultValue = "10") int count)
    {

        PageInfo<View> viewPageInfo=viewService.selectViewWithPage(model,id,viewName,page,count);
        System.out.println(viewPageInfo);
        JsonObject jsonObject=new JsonObject();
        jsonObject.setCode(0);
        jsonObject.setCount(viewPageInfo.getTotal());
        jsonObject.setMsg("");
        jsonObject.setData(viewPageInfo.getList());
        System.out.println(jsonObject);
        return jsonObject;
    }

    //根据id修改视频信息
    @RequestMapping(value = "/admin/editViewInfoById")
    @ResponseBody
    public String updateViewInfoById(View view)
    {
        System.out.println(view);
        Integer rows = viewService.updateViewInfoById(view);
        if(rows>0)
        {
            return "OK";
        }
        return "NO";
    }

    //根据id删除视频
    @RequestMapping(value = "/admin/deleteView")
    @ResponseBody
    public String deleteViewById(Integer id)
    {
        System.out.println(id);
        Integer rows = viewService.deleteViewById(id);
        if(rows>0)
        {
            return "OK";
        }
        return "NO";
    }


    @RequestMapping(value = "/user/toIndex")
    private String index(Model model,@RequestParam(value = "p",defaultValue = "1") int page, @RequestParam(value = "count",defaultValue = "25") int count) {

        PageInfo<View> views=viewService.selectViewWithPageShow(model,page,count);
        model.addAttribute("viewList",views);
        System.out.println(views.toString());
        return "index";
    }
}
